<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>后台管理员</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all">
</head>
<body>
<div class="layui-fluid">
  <div class="layui-card">

    <div class="layui-card-body">

      <table id="demo" class="layui-hide" lay-filter="test"></table>

    </div>
  </div>
</div>
<script type="text/html" id="avatarUrlTpl">
  <input type="hidden" id="avatarUrl" value={{ d.avatarUrl }}>
  <img style="display: inline-block; width: 100%; height: 100%;" src={{ d.avatarUrl }}>
</script>
<script type="text/html" id="qrcodeTpl">
  <input type="hidden" id="qrcode" value={{ d.qrcode }}>
  <img style="display: inline-block; width: 100%; height: 100%;" src={{ d.qrcode }}>
</script>
<script type="text/html" id="barDemo">
  <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="more"><i class="layui-icon layui-icon-list"></i>认证</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
          class="layui-icon layui-icon-delete"></i>删除</a>
</script>
<script type="text/html" id="toolbarDemo">
  <div class="layui-btn-container">
    <button class="layui-btn" lay-event="add">新增</button>
    <button class="layui-btn layui-btn-danger " lay-event="delAll">批量删除</button>
  </div>
</script>
<script type="text/html" id="checkboxTpl">
  <input type="checkbox" name="lock" lay-skin="switch" value="{{d.gender}}" disabled lay-text="男|女" {{ d.gender== 1  ? 'checked' : '' }} >
</script>
<script type="text/html" id="useTpl">

  {{#  if(d.isauth === 1){ }}
  <a class="layui-btn layui-btn-xs">已认证</a>
  {{#  } else if(d.isauth === 0){ }}
  <a class="layui-btn layui-btn-warm layui-btn-xs">未认证</a>
  {{#  } else{ }}
  <a class="layui-btn layui-btn-danger layui-btn-xs">{{ d.isauth }}</a>
  {{#  } }}

</script>
<script src="/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['laydate', 'layer', 'table'], function () {
        var $ = layui.$,
            form = layui.form,
            laydate = layui.laydate,
            table = layui.table,
            layer = layui.layer;

        //表格初始化
        table.render({
            elem: '#demo',
            id: 'idTest',
            url: '/getUsers',
            page: true,
            toolbar: '#toolbarDemo',
            height: 'full-50',
            cols: [
                [ //表头
                    {type: 'checkbox', fixed: 'left'},
                    {
                        field: 'uuid',
                        title: '用户ID',
                        width: 220,
                        sort: true
                    }, {
                    field: 'isauth',
                    title: '是否认证',
                    width: 100,
                    templet: '#useTpl',
                    sort: true,
                    unresize: true
                }, {
                    field: 'openid',
                    title: '微信ID',
                    width: 220
                }, {
                    field: 'nickname',
                    title: '昵称',
                    width: 120
                }, {
                    field: 'avatarUrl',
                    title: '头像',
                    width: 60,
                    templet: "#avatarUrlTpl",
                    event: 'showAvatarUrl'
                }, {
                    field: 'gender',
                    title: '性别',
                    width: 100,
                    templet: '#checkboxTpl',
                    unresize: true
                }, {
                    fixed: 'right', title: '操作', toolbar: '#barDemo', width: 160
                }]
            ]
        });

        //监听头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , checkData = checkStatus.data,
                formData = [];
            switch (obj.event) {
                case 'add':
                    layer.alert("请前往微信公众号平台进行用户的授权");
                    break;
                case 'delAll':
                    if (checkData.length == 0) {
                        layer.msg("不能为空");
                    } else {
                        layer.confirm('是否删除此记录?', {icon: 3, title: '提示'}, function (index) {
                            for (var i = 0; i < checkData.length; i++) {
                                formData.push(checkData[i].uuid);
                            }
                            $.ajax({
                                type: "delete",
                                contentType: 'application/json',
                                url: "/delUser/" + formData,
                                data: {},
                                dataType: 'json',
                                success: function (data) {
                                    if (data.code == 200) {
                                        layer.msg('删除成功',
                                            {
                                                icon: 1,
                                                time: 1000
                                            }, function () {
                                                table.reload('idTest', {
                                                    url: '/getUsers',
                                                    page: {
                                                        curr: 1
                                                    }
                                                });
                                                layer.close(index); //关闭弹层
                                            });
                                    } else {
                                        layer.msg("后台出错");
                                    }
                                }
                            });
                        });
                    }
                    break;
            }
            ;
        });
        //监听工具条
        //监听事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'more':
                    layer.open({
                        type: 2
                        , title: '认证用户'
                        , content: '/authUser/' + data.uuid
                        , maxmin: true
                        , area: ['700px', '450px']
                        , btn: ['同意', '拒绝']
                        , btn2: function (index, layero) {
                            layer.prompt({
                                formType: 2,
                                title: '请输入拒绝的原因',
                                area: ['300px', '150px'] //自定义文本域宽高
                            }, function (value, index, elem) {
                                console.log(data)
                                $.ajax({
                                    type: "get",
                                    contentType: 'application/json',
                                    url: "/user/sendMessage",
                                    data: {
                                        // uuid: data.uuid,
                                        email: data.email,
                                        value: value
                                    },
                                    dataType: 'json',
                                    success: function (data) {
                                        if (data.code == 200) {
                                            layer.msg('信息已发送',
                                                {
                                                    icon: 1,
                                                    time: 1000
                                                }, function () {
                                                    table.reload('idTest', {
                                                        url: '/getUsers',
                                                        page: {
                                                            curr: 1
                                                        }
                                                    });
                                                    layer.close(index);
                                                });
                                        } else {
                                            layer.msg("后台出错");
                                        }
                                    }
                                });
                            });

                        }
                        , yes: function (index, layero) {
                            $.ajax({
                                type: "get",
                                contentType: 'application/json',
                                url: "/user/updateUser",
                                data: {
                                    uuid: data.uuid,
                                    email: data.email
                                },
                                dataType: 'json',
                                success: function (data) {
                                    if (data.code == 200) {
                                        layer.msg('认证成功',
                                            {
                                                icon: 1,
                                                time: 1000
                                            }, function () {
                                                table.reload('idTest', {
                                                    url: '/getUsers',
                                                    page: {
                                                        curr: 1
                                                    }
                                                });
                                                layer.close(index); //关闭弹层
                                            });
                                    } else {
                                        layer.msg("后台出错");
                                    }
                                }
                            });
                        }
                    });
                    break;
                case 'del':
                    layer.confirm('是否删除此记录?', {icon: 3, title: '提示'}, function (index) {
                        $.ajax({
                            type: "delete",
                            contentType: 'application/json',
                            url: "/delUser/" + data.uuid,
                            data: {},
                            dataType: 'json',
                            success: function (data) {
                                if (data.code == 200) {
                                    layer.msg('删除成功',
                                        {
                                            icon: 1,
                                            time: 1000
                                        }, function () {
                                            table.reload('idTest', {
                                                url: '/getUsers',
                                                page: {
                                                    curr: 1
                                                }
                                            });
                                            layer.close(index); //关闭弹层
                                        });
                                } else {
                                    layer.msg("后台出错");
                                }
                            }
                        });
                    });
                    break;
                case 'showAvatarUrl':
                    layer.photos(
                        {photos: {title: "查看头像", data: [{src: data.avatarUrl}]}, shade: .01, closeBtn: 1, anim: 5}
                    );
                    break;
                case 'showQrcode':
                    layer.photos(
                        {photos: {title: "查看二维码", data: [{src: data.qrcode}]}, shade: .01, closeBtn: 1, anim: 5}
                    );
                    break;
            }
            ;
        });
    });
</script>
</body>
</html>
